package cn.net.shuxing.yifu.common.back.controller;

import cn.net.shuxing.yifu.common.bean.DivList;
import cn.net.shuxing.yifu.common.bean.OpenPriceHistory;
import cn.net.shuxing.yifu.common.bean.SurveyList;
import cn.net.shuxing.yifu.common.bean.User;
import cn.net.shuxing.yifu.common.dto.BaseDto;
import cn.net.shuxing.yifu.common.service.DealCommonService;
import cn.net.shuxing.yifu.common.service.OpenPriceHistoryService;
import cn.net.shuxing.yifu.common.service.UserCommonService;
import cn.net.shuxing.yifu.common.util.JedisUtil;
import cn.net.shuxing.yifu.common.util.RedisKeyCreator;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;

@RestController
@RequestMapping("/back/survey")
public class SurveyController {
    @Autowired
    private OpenPriceHistoryService openPriceHistoryService;

    @Autowired
    private DealCommonService dealCommonService;

    @Autowired
    private UserCommonService userCommonService;

    @Autowired
    private JedisUtil jedisUtil;

    @RequestMapping("/survey/list")
    public BaseDto getDivList(@RequestParam(defaultValue = "1", value = "coinTypeId") Integer coinTypeId) {
        BaseDto baseDto = new BaseDto();
        String s = jedisUtil.get(RedisKeyCreator.surverDivlList());
        if (s == null) {
            DivList divList = dealCommonService.getDivList(coinTypeId);
            jedisUtil.set(RedisKeyCreator.surverDivlList(), JSON.toJSONString(divList));
            jedisUtil.expired(RedisKeyCreator.surverDivlList(), 600);
            baseDto.setData(divList);
        } else {
            baseDto.setData(JSON.parse(s));
        }
        return baseDto;
    }


    /**
     * 交易统计
     *
     * @return
     */
    @RequestMapping("/deal/list")
    public BaseDto getDealList() {
        BaseDto baseDto = new BaseDto();
        String s = jedisUtil.get(RedisKeyCreator.surverDealList());
//        String s = "";
        if (s == null) {
            SurveyList surveyList = dealCommonService.getSurveyList();
            jedisUtil.set(RedisKeyCreator.surverDealList(), JSON.toJSONString(surveyList));
            jedisUtil.expired(RedisKeyCreator.surverDealList(), 600);
            baseDto.setData(surveyList);
        } else {
            baseDto.setData(JSON.parse(s));
        }
        return baseDto;
    }


    /**
     * 通过月数查找未交易用户列表
     *
     * @param month
     * @return
     */
    @RequestMapping("/deal/nodealmonth")
    public BaseDto getNoDealMonthUserList(@RequestParam(defaultValue = "1", value = "pageNum") int pageNum,
                                          @RequestParam(defaultValue = "15", value = "pageSize") int pageSize,
                                          Integer month) {
        BaseDto baseDto = new BaseDto();
        //Page page = PageHelper.startPage(pageNum,pageSize);
        List<User> noDealUserByMonth = userCommonService.getNoDealUserByMonth(month);
        PageInfo<User> pageInfo = new PageInfo<>(noDealUserByMonth);
        //pageInfo.setTotal(page.getTotal());
        baseDto.setData(pageInfo);
        return baseDto;
    }

    /**
     * 通过天数查找未交易用户列表
     *
     * @param day
     * @return
     */
    @RequestMapping("/deal/nodealday")
    public BaseDto getNodealDayUserlist(@RequestParam(defaultValue = "1", value = "pageNum") int pageNum,
                                        @RequestParam(defaultValue = "15", value = "pageSize") int pageSize,
                                        Integer day) {
        BaseDto baseDto = new BaseDto();
        //Page page = PageHelper.startPage(pageNum,pageSize);
        List<User> noDealUserByDay = userCommonService.getNoDealUserByDay(day);
        PageInfo<User> pageInfo = new PageInfo<>(noDealUserByDay);
        //pageInfo.setTotal(page.getTotal());
        baseDto.setData(pageInfo);
        return baseDto;
    }

    @RequestMapping("/price/price")
    public BaseDto getSinglePrice(@RequestParam(defaultValue = "1", value = "coinTypeId") int coinTypeId) {
        BaseDto baseDto = new BaseDto();
        BigDecimal openPrice = openPriceHistoryService.getOpenPrice(coinTypeId);
        baseDto.setData(openPrice);
        return baseDto;
    }

    @RequestMapping("/price/priceList")
    public BaseDto getPriceList(@RequestParam(defaultValue = "1", value = "coinTypeId") int coinTypeId) {
        BaseDto baseDto = new BaseDto();
        List<OpenPriceHistory> openPriceHistories = openPriceHistoryService.getsByCoinTypeId(coinTypeId);
        baseDto.setData(openPriceHistories);
        return baseDto;
    }

}
